/**
 * 
 */
package kr.co.insoft.core.repository.template.script.impl;

import java.util.List;

import kr.co.insoft.core.mapper.template.script.ScriptMapper;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.model.template.script.ScriptEntityModel;
import kr.co.insoft.core.repository.template.script.ScriptRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Repository
public class ScriptRepositoryImpl implements ScriptRepository {
	
	@Autowired
	ScriptMapper scriptMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getLists(kr.
	 * co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public List<ScriptEntityModel> getPagingList(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
			Assert.notNull(pagingModel.getCurrentPage());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return scriptMapper.getPagingList(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getTotalCount
	 * (kr.co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public int getTotalCount(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return scriptMapper.getTotalCount(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getLists(kr.
	 * co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public List<ScriptEntityModel> getList(ScriptEntityModel scriptEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getUserid());
			Assert.notNull(scriptEntityModel.getCompanyid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return scriptMapper.getList(scriptEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getScript(
	 * kr.co.insoft.core.model.template.ScriptEntityModel)
	 */

	@Override
	public ScriptEntityModel getScript(
			ScriptEntityModel scriptEntityModel) throws RepositoryException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return scriptMapper.getScript(scriptEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#createScript
	 * (kr.co.insoft.core.model.template.script.ScriptEntityModel)
	 */
	@Override
	public ScriptEntityModel createScript(
			ScriptEntityModel scriptEntityModel) throws RepositoryException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptName());
			Assert.notNull(scriptEntityModel.getUserid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
			scriptEntityModel.setScriptid(scriptMapper.getUuid());
			scriptMapper.createScript(scriptEntityModel);
			return scriptEntityModel;
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#removeScript
	 * (kr.co.insoft.core.model.template.script.ScriptEntityModel)
	 */
	@Override
	public void doRemoveScript(ScriptEntityModel scriptEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
		   scriptMapper.doRemoveScript(scriptEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see kr.co.insoft.core.repository.setting.cloud.CloudRepository#isExistsCloudName(kr.co.insoft.core.model.setting.cloud.CloudEntityModel)
	 */
	@Override
	public int isExistScriptName(ScriptEntityModel scriptEntityModel)
			throws RepositoryException {		
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptName());
			Assert.notNull(scriptEntityModel.getUserid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		
		try {
			return scriptMapper.isExistScriptName(scriptEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#doEditScript
	 * (kr.co.insoft.core.model.template.script.ScriptEntityModel)
	 */
	@Override
	public void doEditScript(ScriptEntityModel scriptEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
		   scriptMapper.doEditScript(scriptEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
